package com.dq.controller.user;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dq.controller.in.user.WxUserQuery;
import com.dq.controller.in.user.request.WxUserRequest;
import com.dq.domain.org.Org;
import com.dq.domain.sys.section.SysSection;
import com.dq.domain.sys.user.user.WxUser;
import com.dq.domain.sys.user.user.centre.UserLableCentre;
import com.dq.exception.customException.AdminException;
import com.dq.service.AllService;
import com.dq.vo.ListDataVo;
import com.dq.vo.Vo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cf
 */

@RestController
@RequestMapping("/sys/wxuser/")
@Api(description = "顾客档案管理")
public class SysWxUserController extends AllService {

    @PostMapping("list")
    @ApiOperation("顾客档案列表")
    public Vo customerList(@RequestBody WxUserQuery wxUserQuery) throws AdminException {
        List<WxUser> lists = new ArrayList<>();
        //测试
        //wxUserQuery.setOrgId(0L);
        Long orgId = wxUserQuery.getOrgId();
        if (null != orgId && 0 == orgId) {
            //获取该用户下所有的门店
            List<Org> listAllByUser = orgService.getListAllByUser();
            for (Org org : listAllByUser) {
                wxUserQuery.setOrgId(org.getId());
                //查询出客户资料
                List<WxUser> list = wxUserService.wxUserList(wxUserQuery);
                lists.addAll(list);
            }
        } else {
            List<WxUser> list = wxUserService.wxUserList(wxUserQuery);
            lists.addAll(list);
        }

        List<WxUser.Vo> toList = new ArrayList<>();
        for (WxUser wxUser : lists) {
            WxUser.Vo wxUserVo = (WxUser.Vo) wxUser.toVO();

            wxUserVo.setLableStrList(wxUserService.getWxUserLable(wxUser));
            wxUserVo.setLableMapList(wxUserService.getWxUserLableObj(wxUser));
            wxUserVo.setPayCount(accountService.accountCount(wxUser.getId().toString()));
            toList.add(wxUserVo);
        }

        return new ListDataVo(toList, wxUserService.wxUserCount(wxUserQuery));
    }

    @PostMapping("add")
    @ApiOperation("顾客档案添加")
    public Vo customerAdd(@RequestBody WxUser wxUser) throws AdminException {
        WxUser save = wxUserService.save(wxUser);

        return new Vo(HttpStatus.OK);
    }

    @PostMapping("edit")
    @ApiOperation("顾客信息修改")
    public Vo customerEdit(@RequestBody WxUser wxUser) throws AdminException {
        wxUserService.update(wxUser, true);
        return new Vo(HttpStatus.OK);
    }

    /**
     * 添加顾客记录, 就是修改顾客信息
     */
    @PostMapping("addRecord")
    @ApiOperation("添加顾客记录")
    public Vo addRecord(@Valid @RequestBody WxUserRequest wxUserRequest) throws AdminException {
        WxUser wxUser = new WxUser();
        wxUser.setId(wxUserRequest.getWxUserId());
        wxUser.setLableIdList(wxUserRequest.getLableIdList());
        wxUser.setRemark(wxUserRequest.getRemark());

        return new Vo(wxUserService.update(wxUser, false));
    }


    @ApiOperation("顾客下拉框选择列表(不分页)")
    @GetMapping("wxUserSelectList")
    public Vo wxUserSelectList() throws AdminException {
        List<WxUser> wxUsers = wxUserRepository.findByDeletedFalse();
        JSONArray array = new JSONArray();
        wxUsers.forEach(wxUser -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", wxUser.getId());
            jsonObject.put("name", wxUser.getUserRealcName());
            array.add(jsonObject);
        });
        return new ListDataVo(array);
    }

    @ApiOperation("顾客下拉框选择列表(根据门店id 不分页)")
    @GetMapping("wxUserByOrgSelectList/{orgId}")
    public Vo wxUserByOrgSelectList(@PathVariable Long orgId) throws AdminException {
        SysSection section = dao.get(SysSection.class, orgId);
        if (section == null) {
            throw new AdminException("门店异常");
        }
        List<WxUser> wxUsers = wxUserRepository.findByDeletedAndSection(false, section);
        JSONArray array = new JSONArray();
        wxUsers.forEach(wxUser -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", wxUser.getId());
            jsonObject.put("name", wxUser.getUserRealcName());
            array.add(jsonObject);
        });
        return new ListDataVo(array);
    }
}
